import request from "@/api";
import { ActivityDescriptor, VersionOptions, WorkflowBlueprint, WorkflowInstance } from "./domain";
import { WorkflowModel } from "./view";
import { mapWorkflowModel } from "./utils";

class FlowChartApi {
  static async queryActivities(): Promise<Array<ActivityDescriptor>> {
    return await request({
      url: `v1/activities`,
      method: "get",
    });
  }

  static async queryWorkflowInstance(id: string): Promise<WorkflowInstance> {
    return await request({
      url: `v1/workflow-instances/${id}`,
      method: "get",
    });
  }

  static async queryWorkflowRegistry(
    id: string,
    versionOptions: VersionOptions
  ): Promise<WorkflowBlueprint> {
    const versionOptionsString = getVersionOptionsString(versionOptions);
    return await request({
      url: `v1/workflow-registry/${id}/${versionOptionsString}`,
      method: "get",
    });
  }

  static async queryModels(workflowInstanceId: string): Promise<WorkflowModel> {
    const activityDescriptors = await FlowChartApi.queryActivities();
    const workflowInstance = await FlowChartApi.queryWorkflowInstance(workflowInstanceId);
    const workflowBlueprint = await FlowChartApi.queryWorkflowRegistry(
      workflowInstance.definitionId,
      {
        version: workflowInstance.version,
      }
    );
    return mapWorkflowModel(workflowBlueprint, workflowInstance, activityDescriptors);
  }
}

export default FlowChartApi;
